<template>
  <!-- 弹出框 -->
  <el-dialog class="dialog_style" title="工日调整" :visible.sync="visible" :close-on-click-modal="false" width="50%">
    <div class="app-container">
      <div id="calendar">
        <!-- 年份 月份 -->
        <div class="month">
          <ul>
            <!--点击会触发pickpre函数，重新刷新当前日期 @click(vue v-on:click缩写) -->
            <li class="year-month">
              <span class="choose-year">{{ year }}年{{ currentMonth }}月 工日调整</span>
            </li>
          </ul>
        </div>
        <!-- 星期 -->
        <ul class="weekdays">
          <li>一</li>
          <li>二</li>
          <li>三</li>
          <li>四</li>
          <li>五</li>
          <li >六</li>
          <li >日</li>
        </ul>
        <!-- 日期 -->
        <ul class="days">
          <!-- 核心 v-for循环 每一次循环用<li>标签创建一天 -->
          <li  v-for="(dayobject,index) in days" style="position: relative;border-bottom: 0.1px solid rgb(226 226 226);border-right:0.1px solid rgb(226 226 226)">
            <span v-if="dayobject.ifrest==='休'" style="position: absolute;right: 2px;top: 2px;color: red;opacity: 0.7;">{{ dayobject.ifrest }}</span>
            <span v-if="dayobject.ifrest==='班'" style="position: absolute;right: 2px;top: 2px;color: #2b9af3;opacity: 0.7;">{{ dayobject.ifrest }}</span>
            <!--本月-->
            <!--如果不是本月  改变类名加灰色-->

            <span v-if="dayobject.day.getMonth()+1 !== currentMonth" class="other-month">{{ dayobject.day.getDate() }}</span>

            <el-popover v-if="dayobject.day.getMonth()+1 === currentMonth"
                        placement="right"
                        title="调整"
                        width="250"
                        trigger="click"
            >
              <el-button @click="transformation(dayobject,index)" :style="{color:(dayobject.ifrest==='休'?'#409eff':'#C0C4CC')}" :disabled="dayobject.ifrest==='班'">转换为班</el-button>
              <el-button @click="transformation(dayobject,index)" :style="{color:(dayobject.ifrest==='班'?'#ffa289':'#C0C4CC')}" :disabled="dayobject.ifrest!=='班'">转换为休</el-button>
              <el-button  style="border: none;width: 80%" slot="reference">{{ dayobject.day.getDate() }}</el-button>
            </el-popover>
          </li>
        </ul>
      </div>
    </div>
    <span slot="footer" class="dialog-footer">
           <el-button class="el-button el-button--primary el-button--mini" @click="close">关闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {setConfig} from '@/api/holiday';
import axios from '@/utils/request';
import { Loading } from 'element-ui';
let loading;
const startLoading = () => {  // 使用Element loading-start 方法
  loading = Loading.service({
    lock: true,
    text: '正在新建项目中 请稍等...',
    background: 'rgba(0, 0, 0, 0.7)'
  });
};

export default {
  name: "Tmpl_m",
  data() {
    return {
      selectdate:"",
      visible:false,
      ppvisible:false,
      currentDay: 1,
      currentMonth: 1,
      currentYear: 1970,
      currentWeek: 1,
      days: [],
      year:0,
    };
  },

  methods: {
    // 取消按钮
    close() {
      this.visible = false;
      this.$emit('success')
    },
    transformation(value,index){
      console.log(value);
      let holidayFlag = 0;
      if(value.ifrest==="班"){
        value.ifrest="";
        value.ifrest="休"
        holidayFlag = 1;
      }else {
        value.ifrest="";
        value.ifrest="班"
        holidayFlag = 0;
      }
      let params = {
        "date": this.formatDateV1(value.day),
        "year": this.year,
        "holidayFlag":holidayFlag
      };
      setConfig(params);
      document.body.click()
      this.$set(this.days, index, value)
    },
    formatDateV1(date) {
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, '0');
      const day = String(date.getDate()).padStart(2, '0');
      return `${year}-${month}-${day}`;
    },
    init(value) {
      this.visible=true;
      this.year=value.year;
      this.currentMonth=value.currentMonth;
      this.days= JSON.parse(JSON.stringify( value.days ));
      for(let obj of this.days){
        obj.day=new Date(obj.day);
      }
    },
    /** 初始化月日历数据 */
    getList(cur) {
      var leftcount=0; //存放剩余数量
      var date;
      if (cur) {
        date = new Date(cur);
      } else {
        var now=new Date();
        var d = new Date(this.formatDate(now.getFullYear() , now.getMonth() , 1));
        d.setDate(35);
        date = new Date(this.formatDate(d.getFullYear(),d.getMonth() + 1,1));
      }
      this.currentDay = date.getDate();
      this.currentYear = date.getFullYear();
      this.currentMonth = date.getMonth() + 1;
      this.currentWeek = date.getDay(); // 1...6,0
      if (this.currentWeek == 0) {
        this.currentWeek = 7;
      }
      var str = this.formatDate(this.currentYear , this.currentMonth, this.currentDay);
      this.days.length = 0;
      // 今天是周日，放在第一行第7个位置，前面6个
      //初始化本周
      for (var i = this.currentWeek - 1; i >= 0; i--) {
        var d = new Date(str);
        d.setDate(d.getDate() - i);
        var dayobject={}; //用一个对象包装Date对象  以便为以后预定功能添加属性
        dayobject.day=d;
        this.days.push(dayobject);//将日期放入data 中的days数组 供页面渲染使用
      }
      //其他周
      for (var i = 1; i <= 35 - this.currentWeek; i++) {
        var d = new Date(str);
        d.setDate(d.getDate() + i);
        var dayobject={};
        dayobject.day=d;
        this.days.push(dayobject);
      }
      for(let dayobject of this.days){
        //判断是否节假日 是休还是班
        if(dayobject.day.getDay()==0||dayobject.day.getDay()==6){
          dayobject["ifrest"]="休";
        }else{
          dayobject["ifrest"]="班";
        }
      }
    },
    pickPre: function(year, month) {
      // setDate(0); 上月最后一天
      // setDate(-1); 上月倒数第二天
      // setDate(dx) 参数dx为 上月最后一天的前后dx天
      var d = new Date(this.formatDate(year , month , 1));
      d.setDate(0);
      this.initData(this.formatDate(d.getFullYear(),d.getMonth() + 1,1));
    },
    pickNext: function(year, month) {
      var d = new Date(this.formatDate(year , month , 1));
      d.setDate(35);
      this.initData(this.formatDate(d.getFullYear(),d.getMonth() + 1,1));
    },
    pickYear: function(year, month) {
      alert(year + "," + month);
    },
    // 返回 类似 2016-01-02 格式的字符串
    formatDate: function(year,month,day){
      var y = year;
      var m = month;
      if(m<10) m = "0" + m;
      var d = day;
      if(d<10) d = "0" + d;
      return y+"-"+m+"-"+d
    },
    update() {
      this.visible = false;
      this.$parent.calendarupdate(this.days);
    },

  }
};
</script>

<style src="../css/index.scss" lang="scss"></style>
<style scoped>
.project-management {
  padding: 24px;
  height: calc(100vh - 84px);
}
.pagination {
  float: right;
  margin: 20px 0;
}
.handle-box {
  margin-bottom: 20px;
}
.handle-input {
  width: 300px;
  display: inline-block;
  margin-right: 10px;
}
.table {
  width: 100%;
  font-size: 14px;
  height: calc(100vh - 84px - 80px - 20px - 52px);
  overflow-y: auto;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
.el-table .cell p {
  margin-bottom: 20px;
}
.el-select {
  width: 100%;
}
.el-dialog .el-dialog__header {
  background-color: #518dce;
}
.el-dialog__title,
.el-dialog__headerbtn .el-dialog__close {
  color: #fff;
}
.el-dialog__footer {
  text-align: center;
}
.dialog-footer .el-button--small {
  width: 150px;
}
.el-button + .el-button {
  margin-left: 40px;
}
.dialog_style /deep/ .el-dialog__body{
  padding: 0 20px 10px;
}

.formItem /deep/ .el-form-item__label {
  background-color: #f0f0f0;
  text-align: center;
  height: 40px;
  line-height: 40px;
  padding: 0;
  border-right: 1px solid #ccc;
}
* {
  box-sizing: border-box;
}

ul {
  list-style-type: none;
}

body {
  font-family: Verdana, sans-serif;
  background: #E8F0F3;
}
#calendar{

  box-shadow: 0 2px 2px 0 rgba(0,0,0,0.14), 0 3px 1px -2px rgba(0,0,0,0.1), 0 1px 5px 0 rgba(0,0,0,0.12);
}
.month {
  width: 100%;
  background: #ffffff;
}

.month ul {
  margin: 0;
  padding: 0;
  display: flex;
  justify-content: space-between;
}

.year-month {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-around;
}


.choose-year {
  padding-left: 20px;
  padding-right: 20px;
}

.choose-month {
  text-align: center;
  font-size: 1.5rem;
}



.month ul li {
  color: #9D9D9D;
  font-size: 13px;
  text-transform: uppercase;
  padding: 10px;
  width: 100%;
}

.weekdays {
  margin: 0;
  padding: 6px 0;
  background-color: #409eff;
  display: flex;
  flex-wrap: wrap;
  color: #FFFFFF;
  justify-content: space-around;
}

.weekdays li {
  display: inline-block;
  width: 13.6%;
  text-align: center;
  font-size: 15px;
}

.days {
  padding: 0;
  background: #FFFFFF;
  margin: 0;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

.days li {
  list-style-type: none;
  display: inline-block;
  width: 14.2%;
  text-align: center;
  padding-bottom: 10px;
  padding-top: 10px;
  font-size: 0.5rem;
  color: #000;
}

.days li .active {
  padding: 6px 10px;
  border-radius: 50%;
  background: #00B8EC;
  color: #fff;
}

.days li .other-month {
  padding: 5px;
  color: gainsboro;
}

.days li:hover {
  background: #e1e1e1;
}
</style>
